Zendesk APIでカレンダーの休日登録をしてみました
ベルリンの半瀬です。こんにちは
はじめに
Zendesk APIで一括スケジュール登録をやってみたので、そのメモです。
Zendesk developers - Core API :Schedules
やってみました。
設定済みのスケジュールを確認
% curl -X GET -u "[email protected]/token:XXXXXXXXXXXXXXXXX" https://hogehogehoge.zendesk.com/api/v2/business_hours/schedules.json | jq . % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 418 100 418 0 0 442 0 --:--:-- --:--:-- --:--:-- 442 { "schedules": [ { "id": 250647, "name": "sandbox - test", "time_zone": "Tokyo", "created_at": "2016-12-04T09:10:24Z", "updated_at": "2016-12-04T09:10:31Z", "intervals": [ { "start_time": 1980, "end_time": 2460 }, { "start_time": 3420, "end_time": 3900 }, { "start_time": 4860, "end_time": 5340 }, { "start_time": 6300, "end_time": 6780 }, { "start_time": 7740, "end_time": 8220 } ] } ], "url": "https://hogehogehoge.zendesk.com/api/v2/business_hours/schedules" }
以下の管理画面に相当します。
IDを指定する場合↓(結果出力は同じなので割愛します)
% curl -X GET -u "[email protected]/token:XXXXXXXXXXXXXXXXX" https://hogehogehoge.zendesk.com/api/v2/business_hours/schedules/250647.json | jq .
スケジュール内の、設定済みの休日を確認
% curl -X GET -u "[email protected]/token:XXXXXXXXXXXXXXXXX" https://hogehogehoge.zendesk.com/api/v2/business_hours/schedules/250647/holidays.json | jq . % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 100 316 100 316 0 0 226 0 0:00:01 0:00:01 --:--:-- 226 { "holidays": [ { "id": 164647, "name": "年末年始おやすみ", "start_date": "2016-12-26", "end_date": "2017-01-03" }, { "id": 166608, "name": "2017年1月09日(月)\t成人の日", "start_date": "2017-01-09", "end_date": "2017-01-09" } ], "url": "https://hogehogehoge.zendesk.com/api/v2/business_hours/schedules/250647/holidays" }
以下の管理画面に相当します。
新しい休日を設定
% curl -X POST -u "[email protected]/token:XXXXXXXXXXXXXXXXX" -H "Content-Type: application/json" https://hogehogehoge.zendesk.com/api/v2/business_hours/schedules/250647/holidays.json -d '{"holiday": {"name": "Christmas", "start_date": "2016-12-25", "end_date": "2016-12-25"}}' | jq . % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 284 100 196 100 88 147 66 0:00:01 0:00:01 --:--:-- 147 { "holiday": { "id": 171867, "name": "Christmas", "start_date": "2016-12-25", "end_date": "2016-12-25" }, "url": "https://hogehogehoge.zendesk.com/api/v2/business_hours/schedules/250647/holidays/171867" }
「Chrismas」が作成されました。
複数の登録はうまく動作しないようです。(少し確認が間に合わず、後日アップデートをしようかとおもいます。)
% curl -X POST -u "[email protected]/token:XXXXXXXXXXXXXXXXX" -H "Content-Type: application/json" https://hogehogehoge.zendesk.com/api/v2/business_hours/schedules/250647/holidays.json -d '{"holiday": [{"name": "rarara", "start_date": "2016-12-27", "end_date": "2016-12-27"},{"name": "lelele", "start_date": "2016-12-28", "end_date": "2016-12-28"}]}' | jq . % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 100 1316 100 1156 100 160 724 100 0:00:01 0:00:01 --:--:-- 724 parse error: Invalid numeric literal at line 1, column 10
その他細かな動作
- 登録済みの休日をアップデート
前の項で作成した休日(id: 171867)で実行% curl -X PUT -u "[email protected]/token:XXXXXXXXXXXXXXXXX" -H "Content-Type: application/json" https://hogehogehoge.zendesk.com/api/v2/business_hours/schedules/250647/holidays/171867.json -d '{"holiday": {"name": "クリスマス", "start_date": "2016-12-25", "end_date": "2016-12-25"}}' | jq . % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 296 100 202 100 94 158 73 0:00:01 0:00:01 --:--:-- 158 { "holiday": { "id": 171867, "name": "クリスマス", "start_date": "2016-12-25", "end_date": "2016-12-25" }, "url": "https://hogehogehoge.zendesk.com/api/v2/business_hours/schedules/250647/holidays/171867" }
-
登録済みの休日を削除
% curl -X DELETE -u "[email protected]/token:XXXXXXXXXXXXXXXXX" -H "Content-Type: application/json" https://hogehogehoge.zendesk.com/api/v2/business_hours/schedules/250647/holidays/171867.json | jq .
日本の休日を登録してみます。
これ
を参考にGoogleカレンダーの「日本の祝日」カレンダーから、Zendesk登録用のjson形式で休日情報を取得します。
以下のような実行結果が得られる pythonスクリプトを用意します。
% python mkzenholidayjson.py Getting the upcoming 150 events {"holiday": {"name": "天皇誕生日", "start_date": "2016-12-23", "end_date": "2016-12-24"}} {"holiday": {"name": "元日", "start_date": "2017-01-01", "end_date": "2017-01-02"}} {"holiday": {"name": "元日 振替休日", "start_date": "2017-01-02", "end_date": "2017-01-03"}} {"holiday": {"name": "成人の日", "start_date": "2017-01-09", "end_date": "2017-01-10"}} {"holiday": {"name": "建国記念の日", "start_date": "2017-02-11", "end_date": "2017-02-12"}} {"holiday": {"name": "春分の日", "start_date": "2017-03-20", "end_date": "2017-03-21"}} {"holiday": {"name": "昭和の日", "start_date": "2017-04-29", "end_date": "2017-04-30"}} {"holiday": {"name": "憲法記念日", "start_date": "2017-05-03", "end_date": "2017-05-04"}} {"holiday": {"name": "みどりの日", "start_date": "2017-05-04", "end_date": "2017-05-05"}} {"holiday": {"name": "こどもの日", "start_date": "2017-05-05", "end_date": "2017-05-06"}} {"holiday": {"name": "海の日", "start_date": "2017-07-17", "end_date": "2017-07-18"}} {"holiday": {"name": "山の日", "start_date": "2017-08-11", "end_date": "2017-08-12"}} {"holiday": {"name": "敬老の日", "start_date": "2017-09-18", "end_date": "2017-09-19"}} {"holiday": {"name": "秋分の日", "start_date": "2017-09-23", "end_date": "2017-09-24"}} {"holiday": {"name": "体育の日", "start_date": "2017-10-09", "end_date": "2017-10-10"}} {"holiday": {"name": "文化の日", "start_date": "2017-11-03", "end_date": "2017-11-04"}} {"holiday": {"name": "勤労感謝の日", "start_date": "2017-11-23", "end_date": "2017-11-24"}} {"holiday": {"name": "天皇誕生日", "start_date": "2017-12-23", "end_date": "2017-12-24"}} {"holiday": {"name": "元日", "start_date": "2018-01-01", "end_date": "2018-01-02"}}
素朴に回します。
% while read line do curl -X POST -u "[email protected]/token:XXXXXXXXXXXXXXXXX" -H "Content-Type: application/json" https://hogehogehoge.zendesk.com/api/v2/business_hours/schedules/250647/holidays.json -d $line | jq . done <<END `PYTHONIOENCODING=utf-8 python mkzenholidayjson.py` END % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 296 100 202 100 94 136 63 0:00:01 0:00:01 --:--:-- 136 { "holiday": { "id": 174448, "name": "天皇誕生日", "start_date": "2016-12-23", "end_date": "2016-12-24" }, "url": "https://hogehogehoge.zendesk.com/api/v2/business_hours/schedules/250647/holidays/174448" } <出力を割愛します> { "holiday": { "id": 174588, "name": "元日", "start_date": "2018-01-01", "end_date": "2018-01-02" }, "url": "https://hogehogehoge.zendesk.com/api/v2/business_hours/schedules/250647/holidays/174588" }
登録完了です。↓
まとめ
Zendesk APIで休日のスケジュール登録を行いました。
現在(2016年12月)のところ、Zendeskで日本の休日を反映させる方法はないようです。
手動で入れていくのは辛いなぁということで、Googleカレンダーから引っ張るようにしてみました。
後はスクリプト化して、lambdaなどでスケジュール実行させれば、ひとまず自動で登録できるようにはなりそうです。
スクリプトは待て次回。
それでは〜。